package com.project.finance.mobile.product.controller;

import com.project.finance.common.po.TbCategory;
import com.project.finance.common.po.TbFinancialOrder;
import com.project.finance.common.po.TbProduct;
import com.project.finance.common.po.TbProductDetail;
import com.project.finance.common.resultconfig.Result;
import com.project.finance.common.resultconfig.ResultGenerator;
import com.project.finance.common.vo.MobileProduct;
import com.project.finance.mobile.product.service.MobileProductService;
import com.sun.org.apache.xerces.internal.xs.datatypes.ObjectList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.persistence.Id;
import java.util.*;

@RestController
public class MobileProductController {


    @Autowired
    private MobileProductService productService;


    /*
     * 查询理财产品列表
     *
     */
    @RequestMapping(value = "/financialProduct", method = RequestMethod.GET)
    public Result financialProduct() {
        //获取的集合
        List<MobileProduct> mobileProducts = productService.listProduct();
        //返回的结果
        Map<Object, Object> resultMap = new HashMap<Object, Object>();
        //产品的集合
        List<TbProduct> productList = new ArrayList<TbProduct>();
        //种类的集合
        List<TbCategory> categoryList = new ArrayList<TbCategory>();
        //new一个种类的集合
        List categoryArray = new ArrayList<String>();


        for (MobileProduct mobileProduct : mobileProducts) {
            TbProduct product = new TbProduct();
            product.setAccessPriod(mobileProduct.getAccessPriod());
            product.setAccessType(mobileProduct.getAccessType());
            product.setMinimumPerchase(mobileProduct.getMinimumPerchase());
            product.setProductId(mobileProduct.getProductId());
            product.setProductName(mobileProduct.getProductName());
            product.setProductRate(mobileProduct.getProductRate());
            product.setCategoryId(mobileProduct.getCategoryId());
            productList.add(product);
            if (!categoryArray.contains(mobileProduct.getCategoryName())) {
                categoryArray.add(mobileProduct.getCategoryName());
                TbCategory category = new TbCategory();
                category.setCategoryId(mobileProduct.getCategoryId());
                category.setCategoryName(mobileProduct.getCategoryName());
                categoryList.add(category);
            }
        }

        //返回的结果集
         List<HashMap<String,Object>> outCategoryList=new ArrayList<HashMap<String, Object>>();
        for (TbCategory category : categoryList) {
           HashMap<String,Object> categoryMap=new HashMap<>();
           categoryMap.put("categoryId",category.getCategoryId());
           categoryMap.put("categoryName",category.getCategoryName());
            List<HashMap<String,Object>> innerrProductList=new ArrayList<HashMap<String,Object>>();
            for (TbProduct product : productList) {
                if(category.getCategoryId().equals(product.getCategoryId())){
                    HashMap<String,Object>innerProductMap=new HashMap<String,Object>();
                    innerProductMap.put("accessPriod",product.getAccessPriod());

                    innerProductMap.put("accessType",product.getAccessType());
                    innerProductMap.put("categoryId",product.getCategoryId());
                    innerProductMap.put("productId",product.getProductId());
                    innerProductMap.put("productRate",product.getProductRate());
                    innerProductMap.put("productName",product.getProductName());

                    innerrProductList.add(innerProductMap);
                    categoryMap.put("product",innerrProductList);
                }
            }
           outCategoryList.add(categoryMap);
        }
        resultMap.put("productList",outCategoryList);

        if (mobileProducts != null) {
            return ResultGenerator.apiSuccess(resultMap);
        } else {
            return ResultGenerator.apiError("获取失败");
        }

    }

    /*
     * 查询理财产品详情
     *
     */
    @RequestMapping(value = "financialProduct/{id}", method = RequestMethod.GET)
    public Result financialProduct(@PathVariable("id") int productId) {
        HashMap<String, Object> productDetails = productService.financialProduct(productId);
        if (productDetails != null) {
            return ResultGenerator.apiSuccess(productDetails);
        } else {
            return ResultGenerator.apiError("获取失败");
        }
    }

    /*
    *
    * @RequestParam("userId") int userId,
                                 @RequestParam("productId") int productId,
                                 @RequestParam("payAmount") int payAmount*/
    /*增加订单*/
    @RequestMapping(value = "financialOrder", method = RequestMethod.POST)
    public Result financialOrder(@RequestBody TbFinancialOrder tbFinancialOrder) {
        int i = productService.insert(tbFinancialOrder);
        if (i > 0) {
            return ResultGenerator.apiSuccess("购买成功");
        } else {
            return ResultGenerator.apiError("购买失败");
        }
    }


}
